#include "udpreceivor.h"

#define UDP_MTU 1500

UdpReceivor::UdpReceivor(QString ip, int port, QObject *p) :
    QObject(p),
    ip(ip),
    port(port)
{
    uSocket = new QUdpSocket;
}

UdpReceivor::~UdpReceivor()
{
    uSocket->close();
    delete uSocket;
}

void UdpReceivor::startRecv()
{
    //read_buffer = new QByteArray();//(UDP_MTU, Qt::Uninitialized);
    uSocket->bind(50000, QUdpSocket::ShareAddress);
    connect(uSocket, SIGNAL(readyRead()), this, SLOT(receive()));
}

QByteArray& UdpReceivor::getRecvBuffer()
{
    return read_buffer;
}

void UdpReceivor::receive()
{
    while(uSocket->hasPendingDatagrams()) {
        read_buffer.resize(uSocket->pendingDatagramSize());
        uSocket->readDatagram(read_buffer.data(), read_buffer.size());
        emit signal_getUdpDataReady();

    }
}
